package com.tzinfo.tzt.activity;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.fragment.HomeFragment;
import com.tzinfo.tzt.fragment.ReleaseFragment;
import com.tzinfo.tzt.fragment.UserFragment;

public class CarPlateActivity extends ActionBarActivity implements HomeFragment.OnFragmentInteractionListener ,ReleaseFragment.ReleaseSuccess{
    public RadioGroup bottombar;
    private int bottombarCheckId;
    private HomeFragment homeFragment;
    private ReleaseFragment releaseFragment;
    private UserFragment userFragment;
    private RadioButton tab_menu_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_plate);
        getSupportActionBar().hide();
        bottombar = (RadioGroup) findViewById(R.id.bottombar_container);
        tab_menu_1 = (RadioButton)findViewById(R.id.tab_menu_1);
        setCurrentItem(R.id.tab_menu_1);
        bottombar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                setCurrentItem(checkedId);
            }
        });
    }

    private void setCurrentItem(int checkedId) {
        Fragment fragment = null;
        switch (checkedId) {
            case R.id.tab_menu_1:
                if (homeFragment == null) homeFragment = new HomeFragment();
                fragment = homeFragment;
                bottombarCheckId = checkedId;

                break;
            case R.id.tab_menu_2:

                releaseFragment = new ReleaseFragment();
                fragment = releaseFragment;
                bottombarCheckId = checkedId;
                break;

            case R.id.tab_menu_3:
                if (userFragment == null)
                    userFragment = new UserFragment();
                fragment = userFragment;
                bottombarCheckId = checkedId;

                break;

        }
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame, fragment).commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.car_plate, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void go() {
        ((RadioButton)(bottombar.getChildAt(0))).setChecked(true);
        // setCurrentItem(R.id.tab_menu_1);
        //   修改 tab_menu_1 的样式
    }
}
